package controllers

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/astaxie/beego"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"

	"conf"
	"models/response"
	"models/twitter"
)

type TwitterController struct {
	beego.Controller
}

func (this *TwitterController) Post() {
	var result response.Response

	uid := this.GetString("uid")
	if uid == "" {
		result.Message = response.Info_Error_ParamError
		result.Ret = response.Code_Error_ParamError
		this.Ctx.WriteString(result.FormatJsonString())
		return
	}

	bid := this.GetString("bid")
	if bid == "" {
		result.Message = response.Info_Error_ParamError
		result.Ret = response.Code_Error_ParamError
		this.Ctx.WriteString(result.FormatJsonString())
		return
	}

	var postJson string
	postJson = fmt.Sprintf("%s", this.Ctx.RequestBody)
	fmt.Println(postJson)
	if postJson == "" {
		result.Message = response.Info_Error_ParamError
		result.Ret = response.Code_Error_ParamError
		this.Ctx.WriteString(result.FormatJsonString())
		return
	}

	var postTwitter twitter.Twiiter
	json.Unmarshal([]byte(postJson), &postTwitter)

	postTwitter.Id = bson.NewObjectId()
	postTwitter.CreateTime = time.Now()

	postTwitter.CreateUserId = uid
	postTwitter.BabyId = bid

	switch postTwitter.Type {
	case twitter.TwiiterTypeMessage:
		if postTwitter.Message == "" {
			result.Message = response.Info_Error_ParamError
			result.Ret = response.Code_Error_ParamError
			this.Ctx.WriteString(result.FormatJsonString())
			return
		}

	case twitter.TwiiterTypeImage:
		if postTwitter.Image.Url == "" {
			result.Message = response.Info_Error_ParamError
			result.Ret = response.Code_Error_ParamError
			this.Ctx.WriteString(result.FormatJsonString())
			return
		}

	case twitter.TwiiterTypeHealth:
		if postTwitter.Health.Height == 0 ||
			postTwitter.Health.Weight == 0 {
			result.Message = response.Info_Error_ParamError
			result.Ret = response.Code_Error_ParamError
			this.Ctx.WriteString(result.FormatJsonString())
			return
		}

	case twitter.TwiiterTypeFirstTimeDo:
		if postTwitter.FirstThing.Thing == "" {
			result.Message = response.Info_Error_ParamError
			result.Ret = response.Code_Error_ParamError
			this.Ctx.WriteString(result.FormatJsonString())
			return
		}

	default:

	}

	session, err := mgo.Dial(config.DataBaseIP)
	if err != nil {
		result.Message = response.Info_Error_OperationFailed
		result.Ret = response.Code_Error_OperationFailed
		this.Ctx.WriteString(result.FormatJsonString())
		return
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	fmt.Println("insert postTwitter info")
	c := session.DB(config.DataBaseName).C(config.DataTableTwitter)
	err = c.Insert(&postTwitter)
	if err != nil {
		fmt.Printf("error:%s\n", err)
		result.Message = response.Info_Error_OperationFailed
		result.Ret = response.Code_Error_OperationFailed
		this.Ctx.WriteString(result.FormatJsonString())
		return
	}

	fmt.Println("quer after insert")
	relustTwitter := twitter.Twiiter{}
	err = c.Find(bson.M{"_id": postTwitter.Id}).One(&relustTwitter)
	if err != nil {
		result.Message = response.Info_Error_OperationFailed
		result.Ret = response.Code_Error_OperationFailed
		this.Ctx.WriteString(result.FormatJsonString())
		return
	}

	result.Message = response.Info_Success
	result.Ret = response.Code_Success
	result.Data = relustTwitter

	this.Ctx.WriteString(result.FormatJsonString())

}

func (this *TwitterController) Get() {
	this.Ctx.WriteString("TwitterController get\n")
}
